package com.adam.example.aggregation.metric;

import com.adam.example.TransportClientHolder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;

import java.net.UnknownHostException;

public class ValueCountAggregationExample {

    public static void main(String[] args) throws UnknownHostException {
        TransportClient transportClient = TransportClientHolder.getInstance().getTransportClient();

        ValueCountAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count("agg").field("price");
        SearchResponse searchResponse = transportClient.prepareSearch("test_books")
                .setSize(0).addAggregation(valueCountAggregationBuilder).get();
        ValueCount valueCount = searchResponse.getAggregations().get("agg");
        System.out.println(valueCount.getValue());

        transportClient.close();
    }

}
